/**
 * 比较两个日期的大小，请保证时间字符串的格式为yyyy-MM-dd或yyyy-MM-dd hh:mm:ss
 * @param date1 日期格式字符串或date
 * @param date2 日期格式字符串或date
 * @returns  d1>d2返回为true， 否则为fasle。
 */
function compareDate(date1, date2) {
	if(date1==null || date2 ==null){
		throw new Error("参数不能为空！");
	}
	var reg = /^\d{4}(\-)\d{2}\1\d{1,2}$|^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;   
	if(typeof date1 == "string"){
		if(!reg.test(date1)){
			throw new Error("date1不是标准时间格式字符串！");
		}
	}
	if(typeof date2 == "string"){
		if(!reg.test(date2)){
			throw new Error("date2不是标准时间格式字符串！");
		}
	}
	var temp1 = null;
	var temp2 = null; 
	if(typeof date1 == "string")
		temp1 = new Date(date1.replace(/-/g, "//"));
	if(typeof date2 == "string")
		temp2 = new Date(date2.replace(/-/g, "//"));
	if(date1 instanceof Date)
		temp1 = date1;
	if(date2 instanceof Date)
		temp2 = date2;
	return temp1 > temp2;
}


/**
 * 比较两个日期所差天数
 * @param date1
 * @param date2
 * @returns
 */
function compareDateDiff(date1, date2) {
	if(date1==null || date2 ==null){
		throw new Error("参数不能为空！");
	}
	var reg = /^\d{4}(\-)\d{2}\1\d{1,2}$|^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;   
	if(typeof date1 == "string"){
		if(!reg.test(date1)){
			throw new Error("date1不是标准时间格式字符串！");
		}
	}
	if(typeof date2 == "string"){
		if(!reg.test(date2)){
			throw new Error("date2不是标准时间格式字符串！");
		}
	}
	var temp1 = null;
	var temp2 = null; 
	if(typeof date1 == "string")
		temp1 = new Date(date1.replace(/-/g, "//"));
	if(typeof date2 == "string")
		temp2 = new Date(date2.replace(/-/g, "//"));
	if(date1 instanceof Date)
		temp1 = date1;
	if(date2 instanceof Date)
		temp2 = date2;
	var day = (temp2.getTime() - temp1.getTime() )/(24*60*60*1000);
	return day; 
}

/*UTC时间转为本地时间*/
function convertUtcTimeToLocalTime(utc_time, lag){
	if(utc_time == '' || utc_time == undefined){
		return "";
	}
	//e.g. Param: utc_time = "2011-09-22T09:13:07.0";	lag = +8;
	//return 2011年9月22日 17:13:07
	var utcdate= new Date(Date.parse(utc_time.substr(0,utc_time.indexOf(".")).replace("T", " ").replace(/-/g, "/")));
	if(utcdate == "NaN" || utcdate == "Invalid Date"){
		return utc_time;
	}
	var localdate = new Date(utcdate.getTime() + lag*3600000);
	return localdate.getFullYear()+"-"+(localdate.getMonth()+1 <10 ?"0"+ (localdate.getMonth()+1) : localdate.getMonth()+1 )+"-"+(localdate.getDate()<10?"0" + localdate.getDate() : localdate.getDate())+" "+(localdate.getHours()<10?"0"+localdate.getHours():localdate.getHours())+":"+(localdate.getMinutes()<10 ? "0"+localdate.getMinutes():localdate.getMinutes())+":"+(localdate.getSeconds()<10 ? "0"+localdate.getSeconds():localdate.getSeconds());
}

/*本地时间转为UTC时间*/
function convertLocalTimeToUtcTime(local_time, lead){
	var reg = /^\d{4}(\-)\d{2}\1\d{1,2}$|^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;
	if(typeof local_time == "string")
		if(!reg.test(local_time)){
			throw new Error("local_time不是标准时间格式字符串！");
		}
	var utc_time = new Date(Date.parse(local_time.replace(/-/g, "//")).getTime() - lead*3600000);
	 
	return  utc_time.getFullYear()+"-"+(utc_time.getMonth()+1)+"-"+utc_time.getDate()+"T"+(utc_time.getHours()<10?"0"+utc_time.getHours():utc_time.getHours())+":"+(utc_time.getMinutes()<10 ? "0"+utc_time.getMinutes():utc_time.getMinutes())+":"+(utc_time.getSeconds()<10 ? "0"+utc_time.getSeconds():utc_time.getSeconds())+"."+lead;
}


/**
 * @returns 获取当前日期时间 格式：yyyy-MM-dd hh:mm:ss
 */
function getCurrentDatetime(){
	var currentDatetime = new Date();
	return currentDatetime.getFullYear()+"-"+(currentDate.getMonth()+1 < 10 ?"0"+(currentDate.getMonth()+1 ):(currentDate.getMonth()+1))+"-"+(currentDate.getDate() < 10 ?"0"+currentDate.getDate() : currentDate.getDate())+" "+(currentDatetime.getHours()<10?"0"+currentDatetime.getHours():currentDatetime.getHours())+":"+(currentDatetime.getMinutes()<10 ? "0"+currentDatetime.getMinutes():currentDatetime.getMinutes())+":"+(currentDatetime.getSeconds()<10 ? "0"+currentDatetime.getSeconds():currentDatetime.getSeconds());
}

/**
 * @returns 获取当前日期 格式：yyyy-MM-dd 
 */
function getCurrentDate(){
	var currentDate = new Date();
	return currentDate.getFullYear()+"-"+ (currentDate.getMonth()+1 < 10 ?"0"+(currentDate.getMonth()+1 ):(currentDate.getMonth()+1))+"-" + (currentDate.getDate() < 10 ?"0"+currentDate.getDate() : currentDate.getDate());
}

/**
 * date类型转换为 yyyy-MM-dd格式
 * @param date Date类型时间
 * @returns date对应的时间
 */
function getDate(date){
	if(!(date instanceof Date)){
		throw new Error('参数不是日期类型！');
	}
		return date.getFullYear() + "-" + ((date.getMonth()+1 < 10 ?"0"+ (date.getMonth()+1) : (date.getMonth())+1)) + "-" + (date.getDate() < 10 ?"0"+date.getDate() : date.getDate());
}

/**
 * date类型转换为 yyyy-MM-dd hh:mm:ss格式
 * @param date
 * @returns {String}
 */
function getDatetime(date){
	if(!(date instanceof Date)){
		throw new Error('参数不是日期类型！');
	}
	return date.getFullYear()+"-"+((date.getMonth()+1 < 10 ?"0"+ (date.getMonth()+1) : (date.getMonth())+1))+"-"+(date.getDate() < 10 ?"0"+date.getDate() : date.getDate())+" "+(date.getHours()<10?"0"+date.getHours():date.getHours())+":"+(date.getMinutes()<10 ? "0"+date.getMinutes():date.getMinutes())+":"+(date.getSeconds()<10 ? "0"+date.getSeconds():date.getSeconds());
}